package com.dayuan.controller;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.dayuan.bean.BackMenu;
import com.dayuan.bean.BackUser;
import com.dayuan.service.AdminUserService;
import com.dayuan.utils.SecurityUtil;
import com.dayuan.vo.ResultVo;

@Controller
@RequestMapping("/home")
public class HomeController {

	@Resource
	private AdminUserService adminUserService;

	// home页面跳转
	@RequestMapping(value = "/jump.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public String jump() {
		return "home";
	}

	/**
	 * 获取首页菜单
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/getList.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo getList(ModelMap modelMap) {
		ResultVo resultVo = new ResultVo();
		BackUser adminUser = SecurityUtil.getBackUser();
		if (adminUser != null || !"".equals(adminUser)) {
			List<BackMenu> adminMenus = adminUserService.getMenu(adminUser.getId());
			resultVo.setCode(1);
			resultVo.setData(adminMenus);
			return resultVo;
		}
		return null;
	}

	/* 上传用户头像 */
	@RequestMapping(value = "/uploadtouxiang.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public String fildUpload(@RequestParam(value = "touxiang") MultipartFile touxiang, ModelMap modelMap,
			HttpServletRequest request) throws Exception {

		System.out.println("开始");

		String path = request.getSession().getServletContext().getRealPath("userpicture");
		String fileName = touxiang.getOriginalFilename();

		
		
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}

		// 保存
		try {
			touxiang.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("fileUrl", request.getContextPath() + "/upload/" + fileName);
		
		int id = SecurityUtil.getBackUser().getId();
		String picture = "userpicture/" + fileName;
		System.out.println(picture);
		adminUserService.updateUserPicture(picture, id);

		return "redirect:jump.shtml";
	}

	/**
	 * 首页用户信息、头像显示
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/getUser.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo getUser(ModelMap modelMap) {
		ResultVo resultVo = new ResultVo();
		BackUser adminUser = SecurityUtil.getBackUser();
		if (adminUser != null || !"".equals(adminUser)) {
			BackUser adminUser2 = new BackUser();
			adminUser2.setUserName(adminUser.getUserName());
			adminUser2.setId(adminUser.getId());
			adminUser2.setPicture(adminUser.getPicture());
			resultVo.setCode(1);
			resultVo.setData(adminUser2);
			return resultVo;
		}
		return null;
	}

	// 上传头像页面跳转
	@RequestMapping(value = "/jumpuserinfo.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public String jumpuserinfo() {
		return "userInfo";
	}

}